/**
 * 
 */
package kr.co.insoft.core.service.template.script.impl;

import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.paging.PagingModel;
import kr.co.insoft.core.model.template.script.ScriptEntityModel;
import kr.co.insoft.core.repository.template.script.ScriptRepository;
import kr.co.insoft.core.service.template.script.ScriptService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

/**
 * @author IN-SOFT Inc. Park Jong Taek(jtpark@in-soft.co.kr)
 * 
 */
@Service
public class ScriptServiceImpl implements ScriptService {
	
	@Autowired
	ScriptRepository scriptRepository;
	
	private static final Logger logger = LoggerFactory
			.getLogger(ScriptServiceImpl.class);

	/*
	 * (non-Javadoc)
	 * 
	 * kr.co.insoft.core.service.setting.template.script.ScriptService#getPagingList
	 */
	@Override
	public PagingModel getPagingList(PagingModel pagingModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			pagingModel.setSearchOrCondition(user.getQueryAuthParam());
			List<ScriptEntityModel> lists = scriptRepository
					.getPagingList(pagingModel);
			int totalCount = scriptRepository.getTotalCount(pagingModel);

			logger.info("{}", pagingModel.getSearchAndCondition());

			return new PagingModel(lists, totalCount, pagingModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	public List<ScriptEntityModel> getList() throws ServiceException {
		try {
			ScriptEntityModel scriptEntityModel = new ScriptEntityModel();
			
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			scriptEntityModel.setAuth((User) auths.getPrincipal());
			return scriptRepository.getList(scriptEntityModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void createScript(ScriptEntityModel scriptEntityModel)
			throws ServiceException {
		try {
			Assert.notNull(scriptEntityModel);
			Assert.notNull(scriptEntityModel.getScriptName());
		} catch (IllegalArgumentException e) {
			throw new ServiceException(ExceptionCode.ARGUMENT_ERROR, e);
		}
		
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			scriptEntityModel.setAuth((User) auths.getPrincipal());
			/* Script 등록 */
			scriptEntityModel = scriptRepository.createScript(scriptEntityModel);
		} catch(Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	/* (non-Javadoc)
	 * kr.co.insoft.core.service.setting.template.script.ScriptService#getScript
	 */
	@Override
	public ScriptEntityModel getScript(String scriptid)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();

			ScriptEntityModel scriptEntityModel = new ScriptEntityModel();
			scriptEntityModel.setScriptid(scriptid);
			scriptEntityModel.setAuth((User) auths.getPrincipal());
			return scriptRepository.getScript(scriptEntityModel); 

		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void doEditScript(ScriptEntityModel scriptEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			scriptEntityModel.setAuth((User) auths.getPrincipal());

			scriptRepository.doEditScript(scriptEntityModel);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void doRemoveScript(ScriptEntityModel scriptEntityModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			scriptEntityModel.setAuth((User) auths.getPrincipal());
			
			scriptRepository.doRemoveScript(scriptEntityModel);
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	public boolean isExistScriptName(ScriptEntityModel scriptEntityModel) 
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			scriptEntityModel.setAuth((User) auths.getPrincipal());
			return scriptRepository.isExistScriptName(scriptEntityModel) > 0;
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}
	
}
